/*************************************************************************
 *
 * $Id: 1wire.h 126 2007-02-10 14:57:23Z alpov $
 * 
 *************************************************************************/         

#ifndef _1WIRE_H
#define _1WIRE_H

/* LOW LEVEL */
extern void OWInit(void);
extern unsigned char OWReset(void);
extern void OWWriteBit(unsigned char bit);
extern unsigned char OWReadBit(void);
extern void OWWriteByte(unsigned char data);
extern unsigned char OWReadByte(void);
extern void OWSendCmd(unsigned char *ROM, unsigned char cmd);
extern char OWFirst(unsigned char *ROM);
extern char OWNext(unsigned char *ROM);
extern void OWCRC(unsigned char x, unsigned char *crc);

/* HIGH LEVEL */
extern void OWConvertAll(unsigned char wait);
extern unsigned char OWReadTemperature(unsigned char *ROM, int *temperature);

extern Mutex OneWireMutex;

/* INTERNAL CONSTANTS everything below this line */

#define ERR_BADCRC      0x8000
#define ERR_BADFAMILY   0x8001

/* Return codes for OWFirst()/OWNext() */
#define OW_BADWIRE      -3
#define OW_BADCRC       -2
#define OW_NOPRESENCE   -1
#define OW_NOMODULES    0
#define OW_FOUND        1

/* General 1 wire commands */
#define OW_SEARCH_ROM_CMD   0xF0
#define OW_READ_ROM_CMD     0x33
#define OW_MATCH_ROM_CMD    0x55
#define OW_SKIP_ROM_CMD     0xCC

/* DS1820 commands */
#define OW_CONVERT_T_CMD    0x44
#define OW_RD_SCR_CMD       0xBE
#define OW_WR_SCR_CMD       0x4E


/* 1-wire delays, check F_CPU is set correctly */
#define DELAY_A _delay_us(6)
#define DELAY_B _delay_us(64)
#define DELAY_C _delay_us(60)
#define DELAY_D _delay_us(10)
#define DELAY_E _delay_us(9)
#define DELAY_F _delay_us(55)
#define DELAY_G
#define DELAY_H _delay_ms(0.480)
#define DELAY_I _delay_us(70) 
#define DELAY_J _delay_ms(0.410)

/* Other */
#define CONVERT_T_DELAY 750

#endif
